package com.lsht.system.service.impl.siwei;

import com.lsht.common.core.domain.model.LoginUser;
import com.lsht.system.domain.siwei.SiweiGroup;
import com.lsht.system.domain.siwei.SiweiPoint;
import com.lsht.system.domain.vo.Group;
import com.lsht.system.domain.vo.Region;
import com.lsht.system.mapper.SysDeptMapper;
import com.lsht.system.mapper.siwei.SiweiGroupMapper;
import com.lsht.system.mapper.siwei.SiweiPointMapper;
import com.lsht.system.service.siwei.SiweiGroupService;
import com.lsht.system.util.NullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 分组管理Service业务层处理
 *
 * @author zyc
 * @date 2024-08-14
 */
@Service
public class SiweiGroupServiceImpl implements SiweiGroupService {
    @Autowired
    private SiweiGroupMapper siweiGroupMapper;

    @Autowired
    private SiweiPointMapper siweiPointMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 新增分组管理
     *
     * @param siweiGroup 分组管理
     * @return 结果
     */
    @Override
    public int genAdd(SiweiGroup siweiGroup) {
        siweiGroup.setCreateTime(new Date());
        return siweiGroupMapper.genAdd(siweiGroup);
    }

    /**
     * 删除分组管理信息
     *
     * @param id 分组管理主键
     * @return 结果
     */
    @Override
    public int genDeleteById(List<String> idList) {
        return siweiGroupMapper.genDeleteById(idList);
    }

    /**
     * 修改分组管理
     *
     * @param siweiGroup 分组管理
     * @return 结果
     */
    @Override
    public int genUpdateById(SiweiGroup siweiGroup) {
        siweiGroup.setUpdateTime(new Date());
        return siweiGroupMapper.genUpdateById(siweiGroup);
    }

    /**
     * 查询分组管理列表
     *
     * @param siweiGroup 分组管理
     * @return 分组管理
     */
    @Override
    public List<SiweiGroup> genQueryList(SiweiGroup siweiGroup) {
        List<String> date = siweiGroup.getDate();
        if (NullUtil.isNotNull(date) && date.size() > 0) {
            siweiGroup.setBeginTime(date.get(0));
            siweiGroup.setEndTime(date.get(1));
        }
        return siweiGroupMapper.genQueryList(siweiGroup);
    }

    /**
     * 根据id查询分组详情信息
     *
     * @param siweiGroup
     * @return
     */
    @Override
    public SiweiGroup genQueryById(SiweiGroup siweiGroup) {
        return siweiGroupMapper.genQueryById(siweiGroup);
    }

    /**
     * 查询分组及点位信息
     *
     * @param siweiGroup
     * @param deptId
     * @param loginUser
     * @return
     */
    @Override
    public List<Region> getGroupPoint(SiweiGroup siweiGroup, Long deptId, LoginUser loginUser) {
//        //此处直接取分组数据了
//        List<GroupPointVo> list = siweiGroupMapper.getGroupList(siweiGroup);
//        list.stream().forEach(li -> {
//            String id = li.getId();
//            //根据id查询每个分组下的点位数据
//            List<PointChildren> pointChildrenList = siweiPointMapper.getPointListByGroupId(id);
//            li.setChildren(pointChildrenList);
//
//        });
//        return list;
        //根据部门ID  判断
        //拿原先的id去和新传的对比，
        List<Long> objects = new ArrayList<>();
        boolean admin = loginUser.getUser().isAdmin();
        if (admin) {
//            objects.add(null);
        } else {
            objects.add(deptId);
        }
        // if (NullUtil.isNotNull(deptId)) {
        //     Long parentDeptId = getParentDeptId(deptId);
        //     objects.add(parentDeptId);
        //
        // }
        List<SiweiPoint> list = siweiPointMapper.getPointList(objects,siweiGroup.getSearchValue());  // 获取 SiweiPoint 列表
        // 按 region 分组，再按 groupId 分组
        Map<String, Map<String, List<SiweiPoint>>> grouped = list.stream()
                .filter(gp -> gp.getRegion() != null && gp.getGroupId() != null)  // 过滤掉 region 或 groupId 为 null 的元素
                .collect(Collectors.groupingBy(
                        SiweiPoint::getRegion,  // 第一层分组：按 region
                        Collectors.groupingBy(
                                SiweiPoint::getGroupId  // 第二层分组：按 groupId
                        )
                ));
        // 创建并填充 Region 和 Group 对象
        List<Region> regions = new ArrayList<>();
        grouped.forEach((regionName, groupByIdMap) -> {
            Region region = new Region();
            region.setName(regionName);
            List<Group> groups = new ArrayList<>();
            groupByIdMap.forEach((groupIdName, groupList) -> {
                Group group = new Group();
                group.setName(groupIdName);
                group.setChildren(groupList);
                groups.add(group);
            });
            region.setChildren(groups);
            regions.add(region);
        });

        // 返回包含所有 Region 的 List
        return regions;

    }

    /**
     * 封装查询上级部门Id方法
     */
    private Long getParentDeptId(Long deptId) {
        return sysDeptMapper.getParentDeptId(deptId);
    }


    /**
     * 大屏查询分组数量及昨日新增数
     *
     * @param siweiGroup
     * @return
     */
    @Override
    public HashMap groupStatistics(SiweiGroup siweiGroup) {
        HashMap<Object, Object> map = new HashMap<>();
        //查询分组数量
        Integer groupNum = siweiGroupMapper.getCountGroupNum();
        map.put("groupNum", groupNum);
        //获取昨日开始和结束时间
        String[] yesterdayStartAndEndTime = SiweiDateUtils.getYesterdayStartAndEndTime();
        //查询分组数量昨日日新增数
        Integer groupAddNum = siweiGroupMapper.getNumByYesterday(yesterdayStartAndEndTime[0], yesterdayStartAndEndTime[1]);
        map.put("groupAddNum", groupAddNum);
        return map;
    }
}
